<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class Visitor extends BaseController {

    public function __construct() {
        parent::__construct();
        $this->load->model('VisitorModel');
    }

    public function chat() {
        $this->load->view('visitor/chat');
    }

    public function heatmap() {
        $this->load->view('visitor/heatmap');
    }

    public function region() {
        $this->load->view('visitor/region');
    }

    public function getIP() {
        echo json_encode($this->VisitorModel->getIP());
    }

    public function getBarChart() {
        echo json_encode($this->VisitorModel->getBarChart());
    }

    public function time() {
        $this->load->view('visitor/time');
    }

    public function getLogList() {
        if ($_POST) {
            $ip = @htmlspecialchars(trim($this->input->post("ip")));
            $time = @htmlspecialchars(trim($this->input->post("time")));
            $time1 = @htmlspecialchars(trim($this->input->post("time1")));
            $time2 = @htmlspecialchars(trim($this->input->post("time2")));
            $webId = @htmlspecialchars(trim($this->input->post("webId")));
            $serviceId = @htmlspecialchars(trim($this->input->post("serviceId")));
            $rows = @htmlspecialchars(trim($this->input->post("rows")));
            $page = @htmlspecialchars(trim($this->input->post("page")));
            $re = $this->VisitorModel->getLog($ip, $time, $time1, $time2, $webId, $serviceId, $rows, $page);
            echo json_encode($re);
        }
    }

    public function download() {

        $ipDownload = @htmlspecialchars(trim($this->input->post("ip")));
        $timeDownload = @htmlspecialchars(trim($this->input->post("time")));
        $time1Download = @htmlspecialchars(trim($this->input->post("time1")));
        $time2Download = @htmlspecialchars(trim($this->input->post("time2")));
        $webId = @htmlspecialchars(trim($this->input->post("webId")));
        $serviceId = @htmlspecialchars(trim($this->input->post("serviceId")));
        $re = $this->VisitorModel->getDownload($ipDownload, $timeDownload, $time1Download, $time2Download, $webId, $serviceId);
        global $str;
        foreach ($re as $val) {
            $str = "ID：" . $val['id'] . "\r\n" . '来源IP：' . $val['ip'] . "\r\n" . "聊天时间：" . $val['time'] . "\r\n" . "客服ID：" . $val['service_id'] . "\r\n" . "客服账号：" . $val['service_account'] . "\r\n" . "上传文件路径：" . $val['filepath'] . "\r\n" . "web客户临时ID：" . $val['cid'] . "\r\n" . "客服人员临时会话ID：" . $val['sid'] . "\r\n" . '聊天内容：' . $val['content'] . "\r\n\r\n";
            echo $str;
        }
        $size = strlen($str);
        $extName = "txt";
        $filename = time() . rand(10000, 99999) . "." . $extName;
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length:$size");
        header("Content-Disposition: attachment;filename=$filename"); // 文件将被称为filename
    }

    public function rowDownload() {
        $record = $this->input->post('record');
        if (!empty($record)) {
            $recordArr = explode("\\r\\n", $record);
            $record = implode("\r\n", $recordArr);
        }
        $size = strlen($record);
        $extName = "txt";
        $filename = time() . rand(10000, 99999) . "." . $extName;
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length:$size");
        header("Content-Disposition: attachment;filename=$filename"); // 文件将被称为filename
        if (empty($record))
            echo '无记录';
        else
            echo $record;
    }

    public function month() {  //计算每个月的访问量的的总数
        $re = $this->VisitorModel->getMonth();
        echo json_encode($re);
    }

    public function day() {  //计算每天的访问量的总数
        $re = $this->VisitorModel->getDay($month);
        echo json_encode($re);
    }

    public function hour() { //计算每个小时的访问量的总数
        $re = $this->VisitorModel->getHour($month, $day);
        echo json_encode($re);
    }

}
